<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 采用TP5助手函数可实现单字母函数M D U等,也可db::name方式,可双向兼容
 * ============================================================================
 * $Author: 当燃 2016-01-09
 */
namespace app\mobile\controller;
use app\common\logic\JssdkLogic;
use app\common\logic\UsersLogic;
use app\common\logic\CartLogic;
use Think\Db;
class Index extends MobileBase {

    public function index(){
        /*
            //获取微信配置
            $wechat_list = M('wx_user')->select();
            $wechat_config = $wechat_list[0];
            $this->weixin_config = $wechat_config;        
            // 微信Jssdk 操作类 用分享朋友圈 JS            
            $jssdk = new \Mobile\Logic\Jssdk($this->weixin_config['appid'], $this->weixin_config['appsecret']);
            $signPackage = $jssdk->GetSignPackage();              
            print_r($signPackage);
        */
//        $hot_goods = M('goods')->where("is_hot=1 and is_on_sale=1")->order('goods_id DESC')->limit(20)->cache(true,TPSHOP_CACHE_TIME)->select();//首页热卖商品
//        $thems = M('goods_category')->where('level=1')->order('sort_order')->limit(9)->cache(true,TPSHOP_CACHE_TIME)->select();
//        $this->assign('thems',$thems);
//        $this->assign('hot_goods',$hot_goods);
//        $favourite_goods = M('goods')->where("is_recommend=1 and is_on_sale=1")->order('goods_id DESC')->limit(20)->cache(true,TPSHOP_CACHE_TIME)->select();//首页推荐商品
//
//        //秒杀商品
//        $now_time = time();  //当前时间
//        if(is_int($now_time/7200)){      //双整点时间，如：10:00, 12:00
//            $start_time = $now_time;
//        }else{
//            $start_time = floor($now_time/7200)*7200; //取得前一个双整点时间
//        }
//        $end_time = $start_time+7200;   //结束时间
//        $flash_sale_list = M('goods')->alias('g')
//            ->field('g.goods_id,f.price,s.item_id')
//            ->join('flash_sale f','g.goods_id = f.goods_id','LEFT')
//            ->join('__SPEC_GOODS_PRICE__ s','s.prom_id = f.id AND g.goods_id = s.goods_id','LEFT')
//            ->where("start_time = $start_time and end_time = $end_time")
//            ->limit(3)->select();
//        $this->assign('flash_sale_list',$flash_sale_list);
//        $this->assign('start_time',$start_time);
//        $this->assign('end_time',$end_time);
//        $this->assign('favourite_goods',$favourite_goods);
        if(strstr($_SERVER['HTTP_USER_AGENT'],'MicroMessenger')){
            $user_temp = session('user');
            if (isset($user_temp['user_id']) && $user_temp['user_id']) {
                $user = M('users')->where("user_id", $user_temp['user_id'])->find();
                if (!$user) {
                    $_SESSION['openid'] = 0;
                    session('user', null);
                }
            }
            if (empty($_SESSION['openid'])) {
                $this->weixin_config = M('wx_user')->find(); //获取微信配置
                $this->assign('wechat_config', $this->weixin_config); 
                if(is_array($this->weixin_config) && $this->weixin_config['wait_access'] == 1){
                    $wxuser = $this->GetOpenid(); //授权获取openid以及微信用户信息
                    
                    $subscribe = $this->is_subscribe($wxuser);

                    session('subscribe', $subscribe['subscribe']);// 当前这个用户是否关注了微信公众号
                    if($subscribe['subscribe']==0){
                        unset($_SESSION['openid']);
                        $this->success('您还没有关注公众号，请关注公众号！', U('mobile/user/gongzhonghao'));
                        exit;
                    }

                    //微信自动登录                             
                    $logic = new UsersLogic();
                    $data = $logic->thirdLogin($wxuser);                                
                    if($data['status'] == 1){
                        session('user',$data['result']);
                        setcookie('user_id',$data['result']['user_id'],null,'/');
                        setcookie('is_distribut',$data['result']['is_distribut'],null,'/');
                        setcookie('uname',$data['result']['nickname'],null,'/');                    
                        // 登录后将购物车的商品的 user_id 改为当前登录的id
                        M('cart')->where("session_id", $this->session_id)->save(array('user_id'=>$data['result']['user_id']));
                        $cartLogic = new CartLogic();
                        $cartLogic->setUserId($data['result']['user_id']);
                        $cartLogic->doUserLoginHandle();// 用户登录后 需要对购物车 一些操作   
                    }
                }
            }

            $this->public_assign();
        }


        $user = session('user');
        $userinfo=M('users')->where(array('user_id'=>$user['user_id']))->find();
        // if($userinfo['level'] != 2){
        //     $this->error('只有普通会员才能购买VIP套餐',U('Home/User/index'));
        // }
        $taocan = M('taocan')->select();
        if(count($taocan)>1){
            foreach ($taocan as $key => $value) {
                $goods_id[]=$value['goods_id'];
            }
            $goods_id=implode(',',$goods_id);
            $cartLists = M('goods')->where("goods_id in($goods_id)")->select();
            foreach ($cartLists as $key => $value) {
                $cartList[$value['goods_id']]=$value;
            }
            foreach ($taocan as $key => $value) {
                $taocan[$key]['original_img']=$cartList[$value['goods_id']]['original_img'];
                $taocan[$key]['price']=$cartList[$value['goods_id']]['shop_price']*$value['goods_num']*$value['taocan_price']/100;
            }
        }elseif(count($taocan)==1) {
            $cartList = M('goods')->where(array("goods_id" => $taocan[0]['goods_id']))->find();
            $taocan[0]['original_img']=$cartList['original_img'];
            $taocan[0]['price']=$cartList['shop_price']*$taocan[0]['goods_num']*$taocan[0]['taocan_price']/100;
        }
       
        $this->assign('taocan', $taocan); // 套餐
        //$this->assign('cartList', $cartList); // 套餐商品
         $hot_goods = M('Goods')->select();
        $this->assign('hot_goods',$hot_goods);
         $this->assign('user',$userinfo);

        $levels=M('user_level')->select();
        foreach ($levels as $key => $value) {
            $level[$value['level_id']]=$value;
        }
        $this->assign('level',$level);

        return $this->fetch();
    }

    //升级VIP订单页面
    public function sjvip(){
        if($this->user_id == 0){
            $this->error('请先登录',U('Home/User/login'));
        }
        
        return $this->fetch();
    }
    /**
     * 分类列表显示
     */
    public function categoryList(){
        return $this->fetch();
    }

    /**
     * 模板列表
     */
    public function mobanlist(){
        $arr = glob("D:/wamp/www/svn_tpshop/mobile--html/*.html");
        foreach($arr as $key => $val)
        {
            $html = end(explode('/', $val));
            echo "<a href='http://www.php.com/svn_tpshop/mobile--html/{$html}' target='_blank'>{$html}</a> <br/>";            
        }        
    }
    
    /**
     * 商品列表页
     */
    public function goodsList(){
        $id = I('get.id/d',0); // 当前分类id
        $lists = getCatGrandson($id);
        $this->assign('lists',$lists);
        return $this->fetch();
    }
    
    public function ajaxGetMore(){
    	$p = I('p/d',1);
    	$favourite_goods = M('goods')->where("is_recommend=1 and is_on_sale=1")->order('goods_id DESC')->page($p,C('PAGESIZE'))->cache(true,TPSHOP_CACHE_TIME)->select();//首页推荐商品
    	$this->assign('favourite_goods',$favourite_goods);
    	return $this->fetch();
    }
    
    //微信Jssdk 操作类 用分享朋友圈 JS
    public function ajaxGetWxConfig(){
    	$askUrl = I('askUrl');//分享URL
    	$weixin_config = M('wx_user')->find(); //获取微信配置
    	$jssdk = new JssdkLogic($weixin_config['appid'], $weixin_config['appsecret']);
    	$signPackage = $jssdk->GetSignPackage(urldecode($askUrl));
    	if($signPackage){
    		$this->ajaxReturn($signPackage,'JSON');
    	}else{
    		return false;
    	}
    }
       
}